import sys
import os
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from langchain.agents import create_openai_functions_agent, AgentExecutor,OpenAIFunctionsAgent
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from utils.ChatLlm import llm
from utils.VerifyInput import validate_user_claim_input
from utils.VerifyInfo import check_user_insurance
from utils.VerifyAmout import create_claim_order
from utils.Pay import alipay_transfer
from utils.OutputParser import FunctionAgentOutputParser


tools = [validate_user_claim_input, check_user_insurance, create_claim_order,alipay_transfer]

def get_insurance_agent():
    prompt = ChatPromptTemplate.from_messages([
        ("system",
         "你是一个专业理赔助手，使用工具协助用户完成理赔流程。你接收到的用户输入是结构化的，不需要重复询问用户信息。请根据工具调用结果逐步判断流程，切勿虚构。"),
        ("human", "以下是用户提供的信息，包括姓名、身份证号、理赔金额和理赔描述：{user_input}"),
        MessagesPlaceholder(variable_name="agent_scratchpad")
    ])


    agent = OpenAIFunctionsAgent(
        tools=tools,
        llm=llm,
        prompt=prompt,
        output_parser=FunctionAgentOutputParser,
    )


    executor = AgentExecutor(
        agent=agent,
        tools=tools,
        verbose=True,
        handle_parsing_errors=True
    )

    return executor

# user_input = {
#     "name": "wmvqnm6979",
#     "id_number": "110101199001010011",
#     "claim_amount": "20000000",
#     "description": "车祸受伤需要治疗"
# }
# result = get_insurance_agent().invoke({
#     "input": "以下是用户提供的信息，包括姓名、身份证号、理赔金额和理赔描述",  # 必须的 input 字段
#     "user_input": user_input
# })
# print(result)